package com.aisino.annotation.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;

public class PersonService {
    @Information(name = "璇璇", age = 20, hobbies = {"看书", "刷抖音"}, address = "湖北宜昌")
    private void outputPersonInfo(Person person) {
        String outputStr = String.format("我是%s,我来自%s,今年%s岁，我的爱好是%s。",
                person.getName(),
                person.getAddress(),
                person.getAge(),
                person.getHobbies());
        System.out.println(outputStr);
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = Class.forName("com.aisino.annotation.test.PersonService")
                .getDeclaredMethod("outputPersonInfo", Person.class);
        //如果这个方法上面没有Information注解给出提示
        if (!method.isAnnotationPresent(Information.class)) {
            System.out.println(method.getName() + "未找到@Information注解");
            return;
        }
        //手动对这个方法进行注解解析
        Person person = new Person();
        //获得注解
        Information information = method.getAnnotation(Information.class);
        person.setName(information.name());
        person.setAge(information.age());
        person.setAddress(information.address());
        person.setHobbies(Arrays.asList(information.hobbies()).toString());
        System.out.println("person = " + person);
        System.out.println("information = " + information);
        PersonService personService = new PersonService();
        method.invoke(personService, person);

        //泛型擦除
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(520);
        integers.add(100);
        Method method1 = integers.getClass().getMethod("add", Object.class);
        method1.invoke(integers, "璇璇呀");
        System.out.println("integers = " + integers);
    }
}
